package wfmanage.action;

import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;
import com.util.MyLog;

import edu.wfmanage.models.Lookup;

public class RegisterUser extends ActionSupport{
	private String commandButton = "";
	private String user_name="";
	private String user_id="";
	private String password="";
	private String email_id="";
	private String phone_no="";
	private String msg="";
	private String checknewusrmsg="";
	
	public String getChecknewusrmsg() {
		return checknewusrmsg;
	}
	public void setChecknewusrmsg(String checknewusrmsg) {
		this.checknewusrmsg = checknewusrmsg;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getCommandButton() {
		return commandButton;
	}
	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	public String getUser_name() {
		return user_name;
	}
	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail_id() {
		return email_id;
	}
	public void setEmail_id(String email_id) {
		this.email_id = email_id;
	}
	
	public String getPhone_no() {
		return phone_no;
	}
	public void setPhone_no(String phone_no) {
		this.phone_no = phone_no;
	}
	public String execute() throws Exception {
		if (this.commandButton == null || this.commandButton.isEmpty()) {
			// first time screen
			return "initial_entry";
		}
		else if (this.commandButton.startsWith("Register")) {
			Lookup check_username= new Lookup();
			check_username=Lookup.findOne("user", "where user_name = '"+user_name+"'");
			String check_name="";
			check_name=check_username.getName();
			if(check_name.equals(user_name)){
				msg="User "+user_name+" already exists. Please choose another.";
				return "initial_entry";
			}
			int result=insert("employee");
			int res= insertUser("user");
			if(result>0 && res >0){
				email mail=new email();
				mail.sendmail(user_name, email_id, "", 0,"NA");
				checknewusrmsg="yes";
      			 msg="Hi "+user_name+", you are registered successfully!! Please login and check. An E-mail Notification is sent to your e-mail id  "+email_id;
      			addActionError(getText(msg));
      	       // return "error";
      			 MyLog.log("User "+user_name+" registered successfully!!");
      		 }
      		 else{
      			 msg="Cannot Register User "+user_name+". Please try again later!!";
      			addActionError(getText(msg));
      			 MyLog.log("Cannot Register User "+user_name+". Please try again later!!");
      		 }
			if(this.user_id == null || this.user_id.isEmpty()){
  				return "displayWF";//"hd_login";
  			 }
  			 else{
  				return "displayWF";//"LTA_login";
  			 }
		}
		return "error";
	}
	public int insert(String table) {
		String insertSQL = "insert into "+table
				+ " (user_id,password,user_name,email_id,LTA_LIMIT,EMP_ROLE) " + "values('" + user_id
				+ "', " + "'"+password +"',"+ "'"+user_name +"'"+",'"+email_id +"',"+"0, '');";
		MyLog.log("insert query"+insertSQL);
		return DB.update(insertSQL);
	}
	public int insertUser(String table) {
		String insertSQL = "insert into "+table
				+ " (user_name,password,email_id,is_current,phone_no) " + "values('"+user_name+"',"+ "'"+password +"'"+",'"+email_id +"',1,'"+phone_no+"');";
		MyLog.log("insert query"+insertSQL);
		return DB.update(insertSQL);
	}
}
